/*  OGAS Wireless-Ver. 使用说明

    
        stop 关闭所有电机，屏幕变红
        flash_edit [variable_name] [value] 将对应的FLASH变量修改为对应值并保存到FLASH,相关变量在flash.c中定义。
*/

#include "zf_common_headfile.h"

uint8 wireless_uart_get_data_buffer[64];
uint16 data_length;

void wireless_check() {
    data_length = wireless_uart_read_buffer(wireless_uart_get_data_buffer, sizeof(wireless_uart_get_data_buffer));
    if(data_length){
        wireless_uart_send_string((const char *)wireless_uart_get_data_buffer);
        wireless_process_command();
        memset(wireless_uart_get_data_buffer, 0, data_length);          // 清空数据
    }
}

void execute_flash_command(const char *arg1, float arg2) {
    for (int i = 0; i < M_T_F_SIZE; ++i) {
        if (strncmp(M_T_F[i].variable_name, arg1, sizeof(M_T_F[i].variable_name)) == 0) {
            *(M_T_F[i].variable_address) = arg2;
            flash_write();
            return;
        }
    }
}

void execute_command(const char *command, const char *arg1, float arg2) {
    if (strcmp(command, "stop") == 0) {
        motor_stop = 1;
        memset(wireless_uart_get_data_buffer, 0, data_length);
        tft180_full(RGB565_RED);
        return;
    } else if(strcmp(command, "flash_read") == 0) {
        flash_read();
    } else if (strcmp(command, "flash_edit") == 0) {
        execute_flash_command(arg1, arg2);
    } else {
        
    }
}

void wireless_process_command() {
    // 用于存储命令和参数的数组
    char command[65] = {0};
    char arg1[65] = {0};
    float arg2 = 0.0f;

    // 遍历缓冲区并分离命令和参数
    int index = 0;
    int arg_index = 0;
    int stage = 0; // 0: 解析命令, 1: 解析第一个参数, 2: 解析第二个参数

    while (index < BUFFER_SIZE && wireless_uart_get_data_buffer[index] != '\0') {
        if (wireless_uart_get_data_buffer[index] == ' ') {
            if (stage == 0) {
                command[arg_index] = '\0';
                stage = 1;
                arg_index = 0;
            } else if (stage == 1) {
                arg1[arg_index] = '\0';
                stage = 2;
                arg_index = 0;
            }
        } else {
            if (stage == 0) {
                if (arg_index < 64) {
                    command[arg_index++] = wireless_uart_get_data_buffer[index];
                }
            } else if (stage == 1) {
                if (arg_index < 64) {
                    arg1[arg_index++] = wireless_uart_get_data_buffer[index];
                }
            } else if (stage == 2) {
                // 第二个参数直接读取到一个临时缓冲区，然后转换为浮点型
                char temp[65] = {0};
                int temp_index = 0;
                while (index < BUFFER_SIZE && wireless_uart_get_data_buffer[index] != '\0' && wireless_uart_get_data_buffer[index] != ' ') {
                    if (temp_index < 64) {
                        temp[temp_index++] = wireless_uart_get_data_buffer[index];
                    }
                    index++;
                }
                temp[temp_index] = '\0';
                arg2 = atof(temp);
                break; // 第二个参数读取完毕，退出循环
            }
        }
        index++;
    }

    // 处理没有第二个参数的情况
    if (stage == 1) {
        arg1[arg_index] = '\0';
    }
    // 调用执行命令函数
    execute_command(command, arg1, arg2);
}
